package data_structure;

import java.util.Comparator;
import java.util.HashSet;
import java.util.PriorityQueue;
import java.util.Set;

public class leetcode_0313 {
    public int nthSuperUglyNumber(int n, int[] primes) {
        PriorityQueue<Long> priorityQueue = new PriorityQueue<>(new Comparator<Long>() {
            @Override
            public int compare(Long o1, Long o2) {
                return o1.compareTo(o2);
            }
        });
        priorityQueue.add(1L);
        Set<Long> set = new HashSet<>();
        set.add(1L);
        Long result = 1L;
        while(n !=0){
            result = priorityQueue.poll();
            for(int prime : primes){
                if(!set.contains(prime*result)){
                    set.add(prime*result);
                    priorityQueue.add(prime*result);
                }
            }
            n=n-1;
        }
        return result.intValue();
    }
}
